#include "cwindowx.h"
#include "v2/xml/xml_device_object.h"

CWindowx::CWindowx(QObject *parent) :
    CKnxControl(parent)
{
}

void CWindowx::parseExp(QString exp)
{
}

void CWindowx::setKnxObject(void *xOBJECT)
{
    //表达式状态
    Xml_DEVICE_OBJECT *object = (Xml_DEVICE_OBJECT *)xOBJECT;

    //发送值
    _sendArray = object->_send.split(",");
    _sendPauseArray = object->_stop_send.split(",");

    QStringList arrys = object->_status.split(",");

    if(arrys.size()>=1)
    {
        QStringList feedList = arrys.at(0).split(":");
        if(feedList.size() >= 3)
        {
            _feedbackDimAddress = arrys.at(0);
            _feedbackDimType =  (DATA_TYPE)feedList.at(1).toUInt();
            _feedbackDimDefine =  (DATA_DEFINE)feedList.at(2).toUInt();

            qDebug() << "CWindowx status: " << _feedbackDimAddress;
        }
    }

}

void CWindowx::readKnxAction()
{
    qDebug() << "CWindowx: read the status";
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackDimAddress.split(":")[0];

    eThread->execList.append(s);

    eThread->start();
}

void CWindowx::execKnxAction(QString action,QString value)
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    QStringList *sendArray = &_sendArray;

    if (value == DEVICE_STATUS_STOP) {
        sendArray = &_sendPauseArray;
    }

    QString sendAddress,sendValue = "0";
    DATA_TYPE sendDataType;
    DATA_DEFINE sendValueDefine;

    for (int i=0; i<sendArray->size(); i++)
    {
        QString dev = sendArray->at(i);
        QStringList devArray = dev.split(":");

        if (devArray.size() > 3)
        {
            sendAddress = devArray.at(0);

            if (sendAddress == "msleep") {
                CExecStruct *s = new CExecStruct();
                s->address = dev;

                eThread->execList.append(s);
            }
            else
            {
                sendDataType = (DATA_TYPE)devArray.at(1).toUInt();
                sendValueDefine = (DATA_DEFINE)devArray.at(2).toUInt();

                QStringList sl = devArray.at(3).split("|");

                if(value == DEVICE_STATUS_STOP)
                {
                    sendValue = devArray.at(3);
                }
                else
                {
                    if(sl.size() == 2)
                    {
                        if(value == DEVICE_STATUS_OFF)
                        {
                            sendValue = sl.at(0);
                        }
                        else
                        {
                            sendValue = sl.at(1);
                        }
                    }
                }

                if(!sendAddress.isEmpty())
                {
                    qDebug() << "CWindowx:" <<  sendAddress << " = " << sendValue;

                    int data_len = 0;
                    memset(_data_buf,0,sizeof(_data_buf));

                    if((data_len = DEVICE_OBJECT::KnxQStringToData(sendValue,sendDataType,sendValueDefine,(_u8 *)_data_buf)) == -1)
                    {
                        qDebug() << "SetDev ret='-1' msg='error data args'" ;
                        continue ;
                    }

                    CExecStruct *s = new CExecStruct();
                    s->address = sendAddress;
                    s->type = sendDataType;
                    s->data = QByteArray((const char *)_data_buf,data_len);

                    eThread->execList.append(s);
                }
            }
        }
    }

    eThread->start();

}

QString CWindowx::valueCheck(QString action,QString value)
{
    float svalue = CKnxControl::parseNumberFromString(value);

    float per = svalue/255;
    return QString::number(per, 'f', 2);
}

QString CWindowx::actionFromStatus(QString address,QString value)
{
    return DEVICE_ACTION_MOVE;
}

void CWindowx::execDeviceAction(QString action,QString value,QString unit)
{
    qDebug() << "=== Audio: curtain " << action << " = " << value;
    //stop
    if(action == DEVICE_ACTION_MOVE)
    {
        execKnxAction(action,value);
    }
}
